package cc.ufinity.networkdiagnose.test;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.util.Log;
import android.widget.TextView;
import cc.ufinity.networkdiagnose.app.GlobalApp;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;

public class Location {
	private Handler handler;

	public LocationClient mLocationClient = null;
	private String mData;
	public MyLocationListenner myListener = new MyLocationListenner();
	TextView mTv;
	
	public Location(Context context,Handler handler){
		this.handler=handler;
		mLocationClient = new LocationClient( context );
		mLocationClient.registerLocationListener( myListener );
		Log.d("locSDK_Demo1", "... Application onCreate... pid=" + Process.myPid());
	}
	 
	
	/**
	 *这个是监听百度地图经纬度的变化事件
	 */
	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return ;
			StringBuffer sb = new StringBuffer(256);
			sb.append("time : ");
			sb.append(location.getTime());
			sb.append("\nerror code : ");
			sb.append(location.getLocType());
			sb.append("\nlatitude : ");
			GlobalApp.longitude=location.getLongitude()+"";
			GlobalApp.latitude=location.getLatitude()+"";
			System.out.println(location.getLatitude()+"-----------经纬度---------"+location.getLongitude());
			sb.append(location.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(location.getLongitude());
			sb.append("\nradius : ");
			sb.append(location.getRadius());
			if (location.getLocType() == BDLocation.TypeGpsLocation){
				sb.append("\nspeed : ");
				sb.append(location.getSpeed());
				sb.append("\nsatellite : ");
				sb.append(location.getSatelliteNumber());
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
			}
			sb.append("\nsdk version : ");
			sb.append(mLocationClient.getVersion());
			//logMsg(sb.toString());
			if(GlobalApp.longitude.equals("0.0")||GlobalApp.longitude.equals("4.9E-324")){
				
			}else{
				Message msg = new Message();
				msg.what=1;
				handler.sendMessage(msg);
			}
		}
		
		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null){
				return ;
			}
			StringBuffer sb = new StringBuffer(256);
			sb.append("Poi time : ");
			sb.append(poiLocation.getTime());
			sb.append("\nerror code : ");
			sb.append(poiLocation.getLocType());
			sb.append("\nlatitude : ");
			sb.append(poiLocation.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(poiLocation.getLongitude());
			sb.append("\nradius : ");
			sb.append(poiLocation.getRadius());
			if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
				sb.append("\naddr : ");
				sb.append(poiLocation.getAddrStr());
			} 
			if(poiLocation.hasPoi()){
				sb.append("\nPoi:");
				sb.append(poiLocation.getPoi());
			}else{				
				sb.append("noPoi information");
			}
			//logMsg(sb.toString());
		if(GlobalApp.longitude.equals("0.0")||GlobalApp.longitude.equals("4.9E-324")){
			
		}else{
			Message msg=new Message();
			msg.what=1;
			handler.sendMessage(msg);
		}
		}
	}
}